/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Car;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.CarService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 字典Controller
 * @author ThinkGem
 * @version 2014-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/car")
public class CarController extends BaseController {

	@Autowired
	private CarService carService;

	@ModelAttribute
	public Car get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return carService.get(id);
		}else{
			return new Car();
		}
	}

	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = {"list", ""})
	public String list(Car car, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<String> typeList = this.carService.findTypeList();
		model.addAttribute("typeList", typeList);
		Page<Car> page = carService.findPage(new Page<Car>(request, response), car);

		model.addAttribute("page", page);
		return "modules/sys/carList";
	}

	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "form")
	public String form(Car car, Model model) {
		model.addAttribute("car", car);
		return "modules/sys/carForm";
	}

	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "save")//@Valid
	public String save(Car car, Model model, RedirectAttributes redirectAttributes) {

		if (!beanValidator(model, car)){
			return form(car, model);
		}
		car.setDeleteTag("0");
		this.carService.save(car);
		addMessage(redirectAttributes, "保存车辆'" + car.getCarNo() + "'成功");
		return "redirect:" + adminPath + "/sys/car/?repage&type="+car.getCarType();
	}

	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "delete")
	public String delete(Car car, RedirectAttributes redirectAttributes) {
		this.carService.delete(car);
		addMessage(redirectAttributes, "删除车辆成功");
		return "redirect:" + adminPath + "/sys/car/?repage&type="+car.getCarType();
	}

//	@RequiresPermissions("user")
//	@ResponseBody
//	@RequestMapping(value = "treeData")
//	public List<Map<String, Object>> treeData(@RequestParam(required=false) String type, HttpServletResponse response) {
//		List<Map<String, Object>> mapList = Lists.newArrayList();
//		Car car = new Car();
//		car.setCarType(type);
//		List<Car> list = this.carService.findList(car);
//		for (int i=0; i<list.size(); i++){
//			Car e = list.get(i);
//			Map<String, Object> map = Maps.newHashMap();
//			map.put("id", e.getId());
//			map.put("pId", e.getParentId());
//			map.put("name", StringUtils.replace(e.getLabel(), " ", ""));
//			mapList.add(map);
//		}
//		return mapList;
//	}

	@ResponseBody
	@RequestMapping(value = "listData")
	public List<Car> listData(@RequestParam(required=false) String type) {
		Car car = new Car();
		car.setCarType(type);
		return this.carService.findList(car);
	}

}
